--[[
    游戏聊天组件
]] --

local blaze = require "blaze"
local app = require "app"
local gamelib = require "club.threecard.gamelib"
local game = app.game.running
local chat = gamelib.chat

local log = blaze.logging.get("Chat")

local M = game.kbe.component("Chat")

function M.__init__(self)
end

function M:say(msg_type, text)
    self:cellCall("say", msg_type, text)
end

function M:on_say(msg_type, text)
    local owner = self.owner
    if msg_type == chat.MSG_TYPE_TEXT then
        log.debug(string.format("%s 说：%s", owner.name, text))
        game.store.chat.new_message({
            sender = owner,
            msg_type = msg_type,
            text = text
        })
    elseif msg_type == chat.MSG_TYPE_PHRASE then
        log.debug(string.format("%s 说（短语）：%s", owner.name, text))
        game.store.chat.new_message({
            sender = owner,
            msg_type = msg_type,
            text = game.i18n.text("chat:phrase" .. text)
        })
    elseif msg_type == chat.MSG_TYPE_EMOJI then
        log.debug(string.format("%s 说（表情）：%s", owner.name, text))
        game.store.chat.new_message({
            sender = owner,
            msg_type = msg_type,
            emoji_id = tonumber(text)
        })
    else
        log.warn(string.format("%s 说（未知消息类型%s）：%s", owner.name, msg_type, text))
    end
end

function M:on_error(code)
    log.debug("发送聊天消息失败：错误码=" .. tostring(code))
end

return M
